package com.zhang.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import com.zhang.yygh.common.exception.YYGHException;
import com.zhang.yygh.hosp.repository.DepartmentRepository;
import com.zhang.yygh.hosp.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        String string = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(string, Department.class);

        Department mongoDepartment=departmentRepository.findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (mongoDepartment==null){ //添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else { //更新
            department.setCreateTime(mongoDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(mongoDepartment.getIsDeleted());
            department.setId(mongoDepartment.getId());
            departmentRepository.save(department);
        }
    }

    //科室分页
    @Override
    public Page getDepartmentPage(Map<String, Object> paramMap) {
        String page = (String) paramMap.get("page");
        int page1 = Integer.parseInt(page);
        String limit = (String) paramMap.get("limit");
        int limit1 = Integer.parseInt(limit);
        String hoscode = (String) paramMap.get("hoscode");
        Pageable pageable= PageRequest.of(page1-1,limit1, Sort.Direction.ASC,"createTime");
        Department department = new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        Example example=Example.of(department);
        //带查询条件的分页
        Page all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void remove(Map<String, Object> paramMap) {
        String string = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(string, Department.class);
        Department department1 = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (department1==null){
            throw new YYGHException(20001,"没有该科室信息");
        }
        departmentRepository.deleteById(department1.getId());
    }

    //查询科室信息
    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example=Example.of(department);
        //所有科室:
        List<Department> all = departmentRepository.findAll(example);
        //key：大科室编号，value：小科室列表
        Map<String, List<Department>> departmentMap = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        ArrayList<DepartmentVo> departmentVoList = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            String bigCode = entry.getKey();
            List<Department> childDepartmentList = entry.getValue();
            //准备大科室数据
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(childDepartmentList.get(0).getBigname());

            ArrayList<DepartmentVo> resultChildDepartmentList = new ArrayList<>();
            //准备小科室数据
            for (Department childDepartment : childDepartmentList) {
                DepartmentVo departmentVoNew = new DepartmentVo();
                departmentVoNew.setDepcode(childDepartment.getDepcode());
                departmentVoNew.setDepname(childDepartment.getDepname());
                //封装到list集合
                resultChildDepartmentList.add(departmentVoNew);
            }
            departmentVo.setChildren(resultChildDepartmentList);

            departmentVoList.add(departmentVo);
        }
        return departmentVoList;
    }
}
